package com.jushu.storbox.database;

import android.database.Cursor;

import com.jushu.storbox.entities.User;

public class UserDao extends BaseDao implements IUserDao {

	public long insertUser(User user) {
		long res = -1;
		User tempUser = getUser(user.getUserId());
		if(tempUser == null) {
			res = getDb().insert(IUserDao.TABLE_NAME, null, user.toContentValues());
		}
		return res;
	}
	
	public User getUser(String loginName, String encryPassword) {
		User user = null;
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(IUserDao.TABLE_NAME);
		sb.append(" where ");
		sb.append(IUserDao.LOGIN_NAME).append("=?");
		sb.append(" and ");
		sb.append(IUserDao.ENCRY_PASSWORD).append("=?");
		Cursor cursor = getDb().rawQuery(sb.toString(),  
				new String[]{loginName, encryPassword});
		if(cursor != null) {
			if(cursor.getCount() > 0 && cursor.moveToFirst()) {
				user = new User();
				user.fromCursor(cursor);
			}
			cursor.close();
		}
		return user;
	}
	
	public User getUser(String ownerId) {
		User user = null;
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(IUserDao.TABLE_NAME);
		sb.append(" where ");
		sb.append(IUserDao.USER_ID).append("=?");
		Cursor cursor = getDb().rawQuery(sb.toString(),  new String[]{ownerId});
		if(cursor != null) {
			if(cursor.getCount() > 0 && cursor.moveToFirst()) {
				user = new User();
				user.fromCursor(cursor);
			}
			cursor.close();
		}
		return user;
	}
	
	public int updateUser(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getUpdateContentValues(), 
			where, 
			new String[]{user.getUserId()});
	}
	
	public int updatePassword(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getEncryPassContentValues(), 
			where, 
			new String[]{user.getUserId()});
	}
	
	public void deleteUser(User user) {
		String where = IUserDao.USER_ID + "=?";
		getDb().delete(IUserDao.TABLE_NAME, 
					where, 
					new String[]{user.getUserId()});
	}
	
	@Override
	public int updateAlbumFolderId(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getFolderIdContentValues(), 
			where, 
			new String[]{user.getUserId()});
	}
	
	@Override
	public int updateAlbumBackFlag(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
				user.getBackupFlagContentValues(), 
				where, 
				new String[]{user.getUserId()});
	}
	
	@Override
	public int updateWifiConfig(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getWifiConfigCV(), 
			where, 
			new String[]{user.getUserId()});
	}
	@Override
	public int updateAlbumEnableFlag(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getAlbumEnableCV(), 
			where, 
			new String[]{user.getUserId()});
	}

	@Override
	public int updateBackStopFlag(User user) {
		String where = IUserDao.USER_ID + "=?";
		return getDb().update(IUserDao.TABLE_NAME, 
			user.getAlbumBackStopCV(), 
			where, 
			new String[]{user.getUserId()});
	}

	@Override
	public long getLastUpdateTime(String objectId) {
		return 0;
	}

	@Override
	public void updateLastUpdateTime(String objectId) {
	}

	

}
